package io.gitee.chearnee.fw.datasecurity.core.util;

import io.gitee.chearnee.fw.datasecurity.core.executor.DefaultCryptExecutor;
import io.gitee.chearnee.fw.datasecurity.core.handler.DefaultAESCodecFieldValueHandler;
import io.gitee.chearnee.fw.datasecurity.core.handler.desensitization.SDRuleHandler;
import io.gitee.chearnee.fw.datasecurity.core.properties.DataSecurityDbCryptProperty;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;

import java.lang.annotation.Annotation;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 11/29/2022 3:58 PM
 */
@RequiredArgsConstructor
public class DataSecurityHelper {
    private static DefaultCryptExecutor defaultCryptExecutor;
    private final DataSecurityDbCryptProperty property;

    @PostConstruct
    public void initDefaultCryptExecutor() {
        defaultCryptExecutor = new DefaultCryptExecutor(new DefaultAESCodecFieldValueHandler(property));
    }

    /**
     * 加密
     */
    public static String encrypt(String object) {
        return defaultCryptExecutor.encrypt(object);
    }

    /**
     * 解密
     */
    public static String decrypt(String object) {
        return defaultCryptExecutor.decrypt(object);
    }

    public static String mask(int firstShow, int lastShow, char mask, String object) {
        return mask().baseMasking(firstShow, lastShow, mask, object);
    }
    public static String mask(String eg, char mask, String object) {
        return mask().baseEgMasking(eg, mask, object);
    }

    @SneakyThrows
    public static <T extends SDRuleHandler<A>, A extends Annotation> SDRuleHandler<A> handler(Class<T> tClass) {
        return tClass.getDeclaredConstructor().newInstance();
    }


    private static SDRuleHandler<Annotation> mask() {
        return new SDRuleHandler<Annotation>() {
            @Override
            public Class<Annotation> getAnnotationType() {
                return null;
            }

            @Override
            public String masking(Annotation annotation, String object) {
                return null;
            }
        };
    }
}
